package com.fz.controller;

import com.alibaba.fastjson.JSONObject;
import com.fz.entity.Book;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by webrx on 2017-09-05.
 */
@Controller
public class Test {
    @RequestMapping("/download")
    public ResponseEntity<byte[]> download(String fn, HttpServletRequest req) throws IOException {
        String fileName = fn;
        String downname = new SimpleDateFormat("yyyyMMddhhmmssS").format(new Date())+fileName.substring(fileName.lastIndexOf(".")).toLowerCase();
        String path = req.getServletContext().getRealPath("/WEB-INF/down/");
        InputStream is = new FileInputStream(path+fileName);
        byte[] buf = new byte[is.available()];
        is.read(buf);
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Disposition", "attchement;filename=" + downname);
        ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(buf, headers, HttpStatus.OK);
        return entity;
    }




    @RequestMapping("/json") @ResponseBody
    public Object ttt(){
        return new String[]{"java","html5","css3","mysql"};
    }

    @RequestMapping("/bk") @ResponseBody
    public Book bk(){
        Book b = new Book();
        b.setId(100);
        b.setPrice(670);
        b.setName("java");
        b.setBdate(new Date());
        return b;
    }

    @RequestMapping("/bks") @ResponseBody
    public List<Book> bks(){
        Book b = new Book();
        b.setId(100);
        b.setPrice(670);
        b.setName("java");
        b.setBdate(new Date());

        List<Book> bks = new ArrayList<Book>();
        bks.add(b);

        Book b2 = new Book();
        b2.setId(200);
        b2.setPrice(70);
        b2.setName("php");
        b2.setBdate(new Date());
        bks.add(b2);
        return bks;
    }

    @RequestMapping("/sss") @ResponseBody
    public String sss(){
        String[] addr = new String[]{"java","php","c++","mysql"};

        //return JSONObject.toJSONString(addr);

        Book b = new Book();
        b.setId(100);
        b.setPrice(670);
        b.setName("java");
        b.setBdate(new Date());

        List<Book> bks = new ArrayList<Book>();
        bks.add(b);

        Book b2 = new Book();
        b2.setId(200);
        b2.setPrice(70);
        b2.setName("php");
        b2.setBdate(new Date());
        bks.add(b2);


        //return JSONObject.toJSONString(bks);

        return JSONObject.toJSONString(b);
    }

    /* ajax string */
    @RequestMapping(value="/timer",method = RequestMethod.POST) @ResponseBody
    public String timer(){
        return new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date());
    }
}
